// pages/register/register.js
const app = getApp();
Page({

  data: {
    userName: '',
    nickName: '',  
    password: '',
    confirmPassword: '',
    phonenumber: '',
    email: '',
    captchaImage: '',
    smsCaptcha: '',
    captchaText: '',
    isLoading: false,
    uuid: '',
    isAgree: false,
    canGetSms: true,
    gender: 0    
  },

  // 输入框事件处理

   // 昵称输入
   onNicknameInput(e) {
    this.setData({ nickName: e.detail.value });
  },
  onUsernameInput(e) {
    this.setData({ userName: e.detail.value });
  },
  // 性别选择
  onGenderChange(e) {
    const gender = parseInt(e.detail.value);
    this.setData({ gender });
  },
  onPasswordInput(e) {
    this.setData({ password: e.detail.value });
  },

  onConfirmPasswordInput(e) {
    this.setData({ confirmPassword: e.detail.value });
  },

  onPhoneInput(e) {
    this.setData({ phonenumber: e.detail.value });
  },

  onEmailInput(e) {
    this.setData({ email: e.detail.value });
  },


  // 切换协议同意状态
  toggleAgree() {
    this.setData({ isAgree: !this.data.isAgree });
  },
  // 注册处理
  handleRegister() {
    const {nickName, userName, password, confirmPassword, phonenumber, email, smsCaptcha, captchaText, uuid, isAgree } = this.data;
    
    // 表单验证
    if (!nickName) {
      wx.showToast({ title: '请输入昵称', icon: 'none' });
      return;
    }
    if (!userName) {
      wx.showToast({ title: '请输入用户名', icon: 'none' });
      return;
    }
    if (!password) {
      wx.showToast({ title: '请输入密码', icon: 'none' });
      return;
    }
    if (password.length < 6) {
      wx.showToast({ title: '密码长度至少6位', icon: 'none' });
      return;
    }
    if (password !== confirmPassword) {
      wx.showToast({ title: '两次输入的密码不一致', icon: 'none' });
      return;
    }
    if (!phonenumber) {
      wx.showToast({ title: '请输入手机号', icon: 'none' });
      return;
    }
    if (!isAgree) {
      wx.showToast({ title: '请同意用户协议和隐私政策', icon: 'none' });
      return;
    }
    
    wx.showLoading({ title: '注册中...' });
    
    // 模拟注册请求
    wx.request({
      url: 'http://localhost:8080/system/user/wxregister',
      method: 'POST',
      data: {
        nickName,
        userName,
        password,
        phonenumber,
        email,
        smsCode: smsCaptcha,
        code: captchaText,
        uuid
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.code === 200) {
          wx.showToast({
            title: '注册成功',
            icon: 'success',
            duration: 1500
          });
          
          // 注册成功后返回登录页
          setTimeout(() => {
            wx.navigateBack();
          }, 1500);
        } else {
          wx.showToast({
            title: res.data.msg || '注册失败',
            icon: 'none'
          });
          this.getCaptcha(); // 刷新验证码
        }
      },
      fail: (err) => {
        wx.hideLoading();
        console.error('注册请求失败:', err);
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      }
    });
  },

  // 返回登录页面
  goToLogin() {
    wx.navigateBack();
  },

  onLoad(options) {
    this.getCaptcha(); // 页面加载时获取验证码
  }
})